package com.wyzz.hr.util;

import com.opensymphony.xwork2.XWorkException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;
import ognl.DefaultTypeConverter;

public class BigDecimalConverter extends DefaultTypeConverter {

	public BigDecimalConverter() {
	}

	private String doConvertToString(Object value) {
		DecimalFormat decimalFormat = new DecimalFormat("##########0.00");
		String result = null;
		if (value instanceof BigDecimal)
			return decimalFormat.format(value);
		else
			return result;
	}

	public Object convertValue(Map ognlContext, Object value, Class toType) {
		Object result = null;
		if (toType == java.math.BigDecimal.class)
			result = doConvertToBigDecimal(value);
		else if (toType == java.lang.String.class)
			result = doConvertToString(value);
		return result;
	}

	private BigDecimal doConvertToBigDecimal(Object value) {
		BigDecimal result = null;
		if (value instanceof String)
			try {
				result = new BigDecimal((String) value);
			} catch (Exception e) {
				throw new XWorkException("Could not parse date", e);
			}
		else if (value instanceof Object[]) {
			Object array[] = (Object[]) (Object[]) value;
			if (array != null && array.length >= 1) {
				value = array[0];
				result = doConvertToBigDecimal(value);
			}
		} else if (java.math.BigDecimal.class
				.isAssignableFrom(value.getClass()))
			result = (BigDecimal) value;
		return result;
	}
}
